import Auth from '@/apis/auth'
import router from '@/router/index.js'

const store = {
  state: {
    user: null,
  },
  getters: {
    username: (state) => (state.user === null ? '未登录' : state.user.username),
    slug: (state) =>
      state.user === null ? '未' : state.user.username.charAt(0),
  },
  mutations: {
    setUser(state, payload) {
      state.user = payload.user
    },
  },
  actions: {
    login({
      commit
    }, {
      username,
      password
    }) {
      return Auth.login({
        username,
        password,
      }).then((res) => {
        commit('setUser', {
          user: res.data,
        })
      })
    },
    logout({
      commit
    }, payload = {
      path: '/login'
    }) {
      return Auth.logout().then((res) => {
        commit('setUser', {
          user: null
        })
        router.push(payload)
      })
    },
    register({
      commit
    }, {
      username,
      password
    }) {
      return Auth.register({
        username,
        password,
      }).then((res) => {
        commit('setUser', {
          user: res.data,
        })
      })
    },
    checkLogin({
      commit,
      state
    }, payload) {
      if (state.user !== null) return Promise.resolve()
      return Auth.getInfo().then((res) => {
        if (!res.isLogin) {
          if (payload) router.push(payload)
        } else {
          commit('setUser', {
            user: res.data,
          })
        }
      })
    },
  },
}

export default store